import { formatTime2View, formatDate2View } from '/@/utils/formatTime';
import EnumOptions from '/@/components/Linxyun/Datas/enum_options.js';
export default function useTablePage() {
	// 处理表格数据
	const getViewData = (fieldDefine: Record<string, any>, value: string) => {
		// console.log('[Debug][useTablePage] getViewData', fieldDefine, value);
		if (fieldDefine.InputType === 'el-date') {
			return formatDate2View(value);
		} else if (fieldDefine.InputType === 'el-time' || fieldDefine.InputType === 'systime') {
			return formatTime2View(value);
		} else if (fieldDefine.InputType === 'el-select') {
			return getSelectValue(fieldDefine, value);
		} else if (fieldDefine.Accuracy.open === '1') {
			return [undefined, null, NaN, ''].includes(value) ? '0.00' : Number(value).toFixed(fieldDefine.Accuracy.digit);
		} else {
			return value;
		}
	};

	// 处理选择器的值
	const getSelectValue = (item: Record<string, any>, currentVal: string) => {
		// console.log('getSelectValue', item, currentVal);
		if (!item.InputDefine || !item.InputDefine.EnumValue) {
			return '--';
		}
		const label = EnumOptions.instance().getOptionLabel(item.InputDefine.EnumValue, currentVal);
		if (label === null) {
			return '--';
		}
		return label;
	};

	const formatUrl = (url: string, prefixUrl: string) => {
		// console.log('formatUrl url', url, url.indexOf(':='));
		let newUrl: string[];
		if (url.indexOf(':=') !== -1) {
			newUrl = url.split(';').map((url) => {
				return prefixUrl + url.split(':=')[1];
			});
		} else if (url.startsWith('http')) {
			newUrl = [url];
		} else {
			newUrl = [prefixUrl + url];
		}

		// console.log('formatUrl', newUrl);
		return newUrl;
	};

	const transformBtnColor = (color: number) => {
		const colorList: ('default' | 'success' | 'text' | 'primary' | 'warning' | 'info' | 'danger')[] = [
			'primary',
			'success',
			'info',
			'warning',
			'danger',
		];
		return colorList[color];
	};
	const transformBtnType = (type: number) => {
		const typeList = ['default', 'plain', 'round', 'circle'];
		return typeList[type];
	};

	const previewFile = (url: string) => {
		window.open(`https://view.xdocin.com/view?src=${url}&watermark=${location.host}`);
	};

	return {
		getViewData,
		formatUrl,
		transformBtnColor,
		transformBtnType,
		previewFile,
	};
}
